// $Id: logrecom_target.js,v 1.8 2009/06/23 09:12:38 jonghoon_kang Exp $

(function($LR){
	if (!window.LogRecommender) {
		window.LogRecommender = $LR = {};
	}
	$LR.Cookie = function(options){this.initialize(options);};
	$LR.Cookie.prototype ={
		initialize: function(options) {
			options = options || {};
			var expires = '';
			if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
				var date;
				if (typeof options.expires == 'number') {
					date = new Date();
					date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
				} else {
					date = options.expires;
				}
				expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
			}
			this.expires = expires;
			this.path = options.path ? '; path=' + options.path : '';
			this.domain = options.domain ? '; domain=' + options.domain : '';
			this.secure = options.secure ? '; secure' : '';
		}
		,cookie: function(name, value) {
			if (!value) {
				var cookieValue = null;
				if (document.cookie && document.cookie != '') {
					var cookies = document.cookie.split(';');
					for (var i = 0; i < cookies.length; i++) {
						var cookie = (cookies[i]||'').replace(/^\s+|\s+$/g, "");
						if (cookie.substring(0, name.length + 1) == (name + '=')) {
							cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
							break;
						}
					}
				}
				return cookieValue;
			} else {
				document.cookie = [name, '=', encodeURIComponent(value), this.expires, this.path, this.domain, this.secure].join('');
			}
		}
		,clear: function(name) {
			document.cookie = [name, '=Deleted;expires=Thu, 01-Jan-1970 00:00:01 GMT;', this.path, this.domain, this.secure].join('');
		}
	};

	var TYPEID_LOG = 0;
	var TYPEID_MIX_HISTORY = 1;
	var TYPEID_MIX_MULTIPLE2 = 2;
	var TYPEID_VIEW_HISTORY = 3;
	var TYPEID_VIEW_MULTIPLE2 = 4;

	var callTargetLogrecom = function(lrtype,accountid,cnt,entryId,productid,days,option) {
		var histCake = new $LR.Cookie({expires:days,path:'/'});
		var name = 'viewhist';
		var history = histCake.cookie(name);
		var histories = [];
		if (history) {
			var histories = history.split('|');
			if (histories.length) {
				if(lrtype==TYPEID_MIX_HISTORY||lrtype==TYPEID_VIEW_HISTORY) {
					$LR.type_history(accountid,histories.slice(0,cnt).join(','),entryId);
				} else if(lrtype==TYPEID_MIX_MULTIPLE2||lrtype==TYPEID_VIEW_MULTIPLE2) {
					$LR.type_multiple2(accountid,histories.join(','),entryId);
				}
				if(lrtype!=TYPEID_VIEW_MULTIPLE2&&lrtype!=TYPEID_VIEW_HISTORY) {
					for (var i = 0; i < histories.length; i++) {
						if (histories[i] == productid) {
							histories.splice(i,1);
						}
					}
				}
			}
		} else if(option && option.forceRequest){
			$LR.type_multiple2(accountid,'',entryId);
		}
		if(histories.length>=cnt) {
			histories.splice(0,1);
		}
		if(lrtype===TYPEID_LOG || lrtype===TYPEID_MIX_HISTORY || lrtype===TYPEID_MIX_MULTIPLE2) {
			if (productid) {
				histories.push(productid);
			}
			histCake.cookie(name,histories.join('|'));
		}
	}
	//lrtype,accountid,cnt,entryId,productid,days
	$LR.target = {
		log:function(accountid,cnt,entryId,productid,days) {
			callTargetLogrecom(TYPEID_LOG,accountid,cnt,entryId,productid,days);
		}
		,mix_history:function(accountid,cnt,entryId,productid,days) {
			callTargetLogrecom(TYPEID_MIX_HISTORY,accountid,cnt,entryId,productid,days);
		}
		,mix_multiple2:function(accountid,cnt,entryId,productid,days) {
			callTargetLogrecom(TYPEID_MIX_MULTIPLE2,accountid,cnt,entryId,productid,days);
		}
		,view_history:function(accountid,cnt,entryId) {
			callTargetLogrecom(TYPEID_VIEW_HISTORY,accountid,cnt,entryId);
		}
		,view_multiple2:function(accountid,cnt,entryId,option) {
			if (option && option.days) {
				callTargetLogrecom(TYPEID_VIEW_MULTIPLE2,accountid,cnt,entryId,null,option.days,option);
			}else {
				callTargetLogrecom(TYPEID_VIEW_MULTIPLE2,accountid,cnt,entryId,null,null,option);
			}
		}
	}
})(window.LogRecommender);
